package com.lamd.myzhxy.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lamd.myzhxy.pojo.Student;
import com.lamd.myzhxy.service.StudentService;
import com.lamd.myzhxy.util.MD5;
import com.lamd.myzhxy.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/sms/studentController")
@Api(tags = "学生管理")
public class StudentController {

    @Autowired
    private StudentService studentService;

    @ApiOperation("删除单个学生或者多个学生信息")
    @DeleteMapping("delStudentById")
    public Result deleteStudent(@RequestBody List<Integer> ids){
        studentService.removeByIds(ids);
        return  Result.ok();
    }


    @ApiOperation("保存或者修改学生信息")
    @PostMapping("/addOrUpdateStudent")
    public Result addOrUpdateStudent(@RequestBody Student student){
        Integer id =student.getId();
        if(id== null || id == 0){
            student.setPassword(MD5.encrypt(student.getPassword()));
        }
        studentService.saveOrUpdate(student);
        return Result.ok();
    }

    @ApiOperation("分页带模糊查询")
    @GetMapping("/getStudentByOpr/{pageNo}/{pageSize}")
    public Result getStudentByOpr(
            @PathVariable("pageNo") Integer pageNo,
            @PathVariable("pageSize") Integer pageSize,
            Student student
    ){

        Page<Student> page = new Page<>(pageNo,pageSize);
        IPage<Student> studentPage = studentService.getSutdentByOpr(page,student);
        return Result.ok(studentPage);
    }
}
